package com.bei_en.teacher.admin.controller.consultation;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bei_en.teacher.admin.model.consultation.bean.ConsultationComment;
import com.bei_en.teacher.admin.model.consultation.condition.ConsultationCommentCondition;
import com.bei_en.teacher.admin.model.consultation.condition.NEWConsultationCommentCondition;
import com.bei_en.teacher.admin.model.consultation.vo.ConsultationCommentVo;
import com.bei_en.teacher.admin.model.users.vo.UsersDetailsVo;
import com.bei_en.teacher.admin.service.consultation.ConsultationCommentService;
import com.bei_en.teacher.admin.service.users.UsersDetailsService;
import com.bei_en.teacher.admin.controller.BaseController;
import com.bei_en.common.util.interceptor.OAuthRequired;
import com.bei_en.common.web.JsonFailResult;
import com.bei_en.common.web.JsonResult;
import com.bei_en.common.web.JsonSuccessResult;
import com.mysql.fabric.xmlrpc.base.Array;

/**
 * 
 * @author Guocg
 * @date 2016-8-4 0:18:25
 */
@Controller
@RequestMapping("/api/Consultation")
public class ConsultationCommentAPPController extends BaseController {

	@Autowired
	ConsultationCommentService consultationCommentService;
    @Autowired
    UsersDetailsService usersDetailsService;
	@RequestMapping("/consultationComment")
	@ResponseBody
	public JsonResult consultationComment(Model model,
			@Valid ConsultationCommentCondition vo, BindingResult br,
			HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());

		try {

			List<ConsultationCommentVo> list = new ArrayList<ConsultationCommentVo>();
			list = consultationCommentService.findConsultationCommentPage(vo);
			JSONArray array=new JSONArray();
			JSONObject object=null;
			ConsultationCommentVo consultationCommentVo=null;
			for (int i = 0; i < list.size(); i++) {
				consultationCommentVo=list.get(i);
				object=new JSONObject();
				object.put("content", consultationCommentVo.getContent());
				object.put("create_time", consultationCommentVo.getCreateTime().getTime());
				//获取评论人
				UsersDetailsVo  usersDetailsVo=	usersDetailsService.findById(consultationCommentVo.getCommentPeople());
				if(usersDetailsVo.getNickname()!=null){
					object.put("name", usersDetailsVo.getNickname());
				}else{
					object.put("name","");
				}
				if(usersDetailsVo.getFacePath()!=null){
					object.put("facePath", usersDetailsVo.getFacePath());
				}else{
					object.put("facePath","");
				}
				array.add(object);
			}
			return new JsonSuccessResult(session.getId(), array);

		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

	@RequestMapping("/consultationCommentAdd")
	@ResponseBody
	@OAuthRequired
	public JsonResult consultationCommentAdd(Model model,
			@Valid NEWConsultationCommentCondition vo, BindingResult br,
			HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
		try {
				
			ConsultationCommentVo consultationComment = new ConsultationCommentVo();
			consultationComment.setConsultationId(vo.getConsultationId());
			consultationComment.setContent(vo.getContent());
			consultationComment.setCommentPeople(vo.getCommentPeople());
			consultationComment.setUserId(vo.getUserId());
			consultationComment.setCreateTime(new Date());
			consultationCommentService.insert(consultationComment);
			return new JsonSuccessResult(session.getId(), "成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

	@RequestMapping("/consultationCommentEdit")
	public JsonResult consultationCommentEdit(Model model,
			@Valid ConsultationCommentCondition vo, BindingResult br,
			HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());

		try {
			ConsultationCommentVo consultationComment = new ConsultationCommentVo();

			consultationCommentService.updateSelective(consultationComment);

			return new JsonSuccessResult(session.getId(), "成功");

		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

}
